#include<bits/stdc++.h>
using namespace std;
#define int long long
const int inf=1e18;
pair<int,int> bfs(int n, vector<vector<int> >& adi, vector<bool>& b, vector<bool>& t){
vector<int> dist(n, -1);
deque<int> q;
q.push_back(0);
dist[0]=0;
while(!q.empty()){
int v=q.front();
q.pop_front();
if(t[v]) return {v, dist[v]};
if(v!=0 && !b[v]) continue;
for(auto u: adi[v]){
if(dist[u]==-1){
dist[u]=dist[v]+1;
q.push_back(u);
}
}
}
return {-1, -1};
}
int longest(int n, vector<vector<int> >& adi, vector<bool>& b, vector<bool>& t){
vector<bool> tok(n, false);
for(int v=0; v<n; v++){
for(auto u: adi[v]){
if(t[u]) tok[v]=true;
}
if(b[v] && tok[v]){
for(auto u: adi[v]){
if(b[u]){
return inf;
}
}
}
}
int cnt=0;
for(int v=0; v<n; v++){
if(!t[v]) continue;
for(auto u: adi[v]){
if(b[u]){
cnt++;
break;
}
}
}
return cnt;
}
bool possible(int d, int l){
if(d==-1) return false;
return l>=(d-1);
}
void solve(){
int n, m;
cin >> n >> m;
int tok, bon;
cin >> tok >> bon;
vector<bool> t(n, false);
vector<bool> b(n, false);
for(int i=0; i<tok; i++){
int x;
cin >> x;
t[x-1]=true;
}
for(int i=0; i<bon; i++){
int x;
cin >> x;
b[x-1]=true;
}
vector<vector<int> > adi(n);
for(int i=0; i<m; i++){
int a, b;
cin >> a >> b;
a--; b--;
adi[a].push_back(b);
adi[b].push_back(a);
}
auto [v, d]=bfs(n, adi, b, t);
if(v!=-1) t[v]=false;
int l=longest(n, adi, b, t);
if(possible(d, l)){
cout << "YES\n";
return;
}
auto [v2, d2]=bfs(n, adi, b, t);
if(v!=-1) t[v]=true;
if(v2!=-1) t[v2]=false;
int l2=longest(n, adi, b, t);
if(possible(d2, l2)){
cout << "YES\n";
}
else{
cout << "NO\n";
}
}
signed main(){
ios_base::sync_with_stdio(false);
cin.tie(0);
int t;
cin >> t;
while(t--){
solve();
}
}
742A - Arpa’s hard exam and Mehrdad’s naive cheat | 1492A - Three swimmers |
1360E - Polygon | 1517D - Explorer Space |
1230B - Ania and Minimizing | 1201A - Important Exam |
676A - Nicholas and Permutation | 431A - Black Square |
474B - Worms | 987B - High School Become Human |
1223A - CME | 1658B - Marin and Anti-coprime Permutation |
14B - Young Photographer | 143A - Help Vasilisa the Wise 2 |
320A - Magic Numbers | 1658A - Marin and Photoshoot |
514A - Chewbaсca and Number | 382A - Ksenia and Pan Scales |
734B - Anton and Digits | 1080A - Petya and Origami |
1642D - Repetitions Decoding | 1440A - Buy the String |
1658F - Juju and Binary String | 478A - Initial Bet |
981A - Antipalindrome | 365A - Good Number |
1204B - Mislove Has Lost an Array | 1409D - Decrease the Sum of Digits |
1476E - Pattern Matching | 1107A - Digits Sequence Dividing |